package test.design_pattern.castle_game.step3;

import test.design_pattern.castle_game.step3.handler.ByeHandler;
import test.design_pattern.castle_game.step3.handler.CmdHandler;
import test.design_pattern.castle_game.step3.handler.GoHandler;
import test.design_pattern.castle_game.step3.handler.HelpHandler;

import java.util.HashMap;
import java.util.Scanner;

public class Game {

    private Room currentRoom;

    private HashMap<String, CmdHandler> cmdHandlers = new HashMap<>();

    public Game() {
        createRooms();
    }

    private void createRooms() {

        cmdHandlers.put("help",new HelpHandler());
        cmdHandlers.put("bye",new ByeHandler());
        cmdHandlers.put("go",new GoHandler(this));

        Room outside, lobby, pub, study, bedroom;

        //	制造房间
        outside = new Room("城堡外");
        lobby = new Room("大堂");
        pub = new Room("小酒吧");
        study = new Room("书房");
        bedroom = new Room("卧室");

        //public void setExits(Room north, Room east, Room south, Room west)
        //	初始化房间的出口
        //outside.setExits(null, lobby, study, pub);
        outside.setExits(Room.EAST, lobby);
        outside.setExits(Room.SOUTH, study);
        outside.setExits(Room.WEST, pub);

        //lobby.setExits(null, null, null, outside);
        lobby.setExits(Room.WEST, outside);

        //pub.setExits(null, outside, null, null);
        pub.setExits(Room.EAST, outside);

        //study.setExits(outside, bedroom, null, null);
        study.setExits(Room.NORTH, outside);

        //bedroom.setExits(null, null, null, study);
        bedroom.setExits(Room.WEST, outside);

        //	从城堡门外开始
        currentRoom = outside;
    }

    private void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到城堡！");
        System.out.println("这是一个超级无聊的游戏。");
        System.out.println("如果需要帮助，请输入 'help' 。");
        System.out.println();
        showPrompt();
    }

    public void goRoom(String direction) {
        Room nextRoom = currentRoom.goRoom(direction);
        if (nextRoom == null) {
            System.out.println("那里没有门！");
        } else {
            showPrompt();
        }
    }

    public void showPrompt(){
        System.out.println("你在" + this.currentRoom);
        System.out.print("出口有: " + this.currentRoom.getExitDesc());
        System.out.println();
    }

    public CmdHandler getHandler(String cmd){
        return this.cmdHandlers.get(cmd);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Game game = new Game();
        game.printWelcome();

        while (true) {
            String line = in.nextLine();
            String[] words = line.split(" ");
            String cmd = words[0];
            CmdHandler cmdHandler = game.getHandler(cmd);
            if(cmdHandler != null){
                if(cmdHandler.isBye()){
                    System.out.println("感谢您的光临。再见！");
                    in.close();
                }
                else{
                    cmdHandler.doCmd(words[1]);
                }
            }
        }
    }

}
